<?php

class Control_SubPageController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout('admin');
        $this->yesButton = 'Yes'; //напис на кнопці Так під час видалення
        $this->noButton = 'No'; //напис на кнопці Ні під час видалення
        $this->subPageDb = new Control_Model_Table_SubPage();
        $this->subContentDb = new Control_Model_Table_SubPageContent();
        $this->view->controller = $this->getRequest()->getControllerName();
        $this->view->action = $this->getRequest()->getActionName();
    }

    public function indexAction() {
        $this->_helper->redirector('edit-pages');
    }

    public function editPagesAction() {
        $page = (int)$this->_getParam('page', 1);
        if ($page>3) {$page==3;} elseif ($page<1) {$page==1;};
        $this->view->PortfolioData = $this->subPageDb->getAllPages();
        $this->view->subPages = $this->subContentDb->getAllPages();
        $form = new Control_Model_Form_SubPage();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        $delparam = $this->_getParam('del', 0);
        ///---------------------------------------------------------------------//
        if ($this->getRequest()->isPost() && ($delparam != 1)) { //Якщо відправлена форма без видалення
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('id');
                $title = $form->getValue('title');
                $name = $form->getValue('name');
                $about_page = $form->getValue('about_page');
                $meta_desc = $form->getValue('meta_desc');
                $meta_keyw = $form->getValue('meta_keyw');
                $block_first = $form->getValue('block_first');
                $foreach_first = $form->getValue('foreach_first');
                $foreach_last = $form->getValue('foreach_last');
                $block_second = $form->getValue('block_second');
                $showin_about = $form->getValue('showin_about');
                $showin_services = $form->getValue('showin_services');
                $showin_news = $form->getValue('showin_news');
                $this->subPageDb->updatePage($id, $title, $name, $about_page,
                        $meta_desc, $meta_keyw, $block_first, $foreach_first,
                        $foreach_last, $block_second, $showin_about,
                        $showin_services, $showin_news);
                $this->_helper->redirector('edit-pages');
            } else {
                $form->populate($formData);
            }
        } elseif ($this->getRequest()->isPost() && ($delparam == 1)) { //Якщо відправлена форма з видаленням
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');
                if ($del == $this->yesButton) {
                    $id = $this->getRequest()->getPost('id');
                    $this->subPageDb->deletePage($id);
                } else {
                    $this->_helper->redirector('edit-pages');
                }
                $this->_helper->redirector('edit-pages');
            }
        } else {//Виведення сторінки едіт
            $id = (int) $this->_getParam('id', 0);
            $del = (int) $this->_getParam('del', 0);
            $this->view->get_id = $id;
            if ($id > 0 && $del == 0) {
                $formdata = $this->subPageDb->getPage($id);
                if (isset($formdata)) {
                    $form->populate($formdata[0]);
                } else {
                    $this->view->get_id = 0;
                };
            } elseif ($id > 0 && $del == 1) {
                $this->view->get_del = 1;
                $this->view->yesButton = $this->yesButton;
                $this->view->noButton = $this->noButton;
                $this->view->NameToDelete = $this->subPageDb->getPage($id);
            } elseif ($id == 0) {
                $this->view->get_id = $id;
            } else {
                $this->view->get_id = 0;
                echo "<span style='color: red;'>Error id: $id<br></span>";
            }
        }
    }

    public function editContentAction() {
        
        $this->view->PortfolioData = $this->subContentDb->getAllPages();
        $form = new Control_Model_Form_SubPageContent();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        $delparam = $this->_getParam('del', 0);
        ///---------------------------------------------------------------------//
        if ($this->getRequest()->isPost() && ($delparam != 1)) { //Якщо відправлена форма без видалення
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('id');
                $id_page = $form->getValue('id_page');
                $content = $form->getValue('content');
                $this->subContentDb->updateContent($id, $id_page, $content);
                $this->_helper->redirector('edit-pages','sub-page','control', array('id' => $form->getValue('id_page')));
            } else {
                $form->populate($formData);
            }
        } elseif ($this->getRequest()->isPost() && ($delparam == 1)) { //Якщо відправлена форма з видаленням
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');
                $id = $this->getRequest()->getPost('id');
                $redirrect=$this->subContentDb->getPageContentId($id);
                if ($del == $this->yesButton) {
                    $this->subContentDb->deleteContent($id);
                } else {
                    $this->_helper->redirector('edit-pages','sub-page','control', array('id' => $redirrect['0']['id_page']));
                }
                $this->_helper->redirector('edit-pages','sub-page','control', array('id' => $redirrect['0']['id_page']));
            }
        } else {//Виведення сторінки едіт
            $id = (int) $this->_getParam('id', 0);
            $del = (int) $this->_getParam('del', 0);
            $this->view->get_id = $id;
            if ($id > 0 && $del == 0) {
                $formdata = $this->subContentDb->getPageContentId($id);
                if (isset($formdata)) {
                                      //  Zend_Debug::dump($formdata);exit();
                    $form->populate($formdata[0]);
                } else {
                    $this->view->get_id = 0;
                };
            } elseif ($id > 0 && $del == 1) {
                $this->view->get_del = 1;
                $this->view->yesButton = $this->yesButton;
                $this->view->noButton = $this->noButton;
                $this->view->NameToDelete = $this->subContentDb->getPageContentId($id);
            } elseif ($id == 0) {
                $this->view->get_id = $id;
            } else {
                $this->view->get_id = 0;
                echo "<span style='color: red;'>Error id: $id<br></span>";
            }
        }
    }

    public function addPagesAction() {
        $form = new Control_Model_Form_SubPage();
        $form->submit->setLabel('Add');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $title = $form->getValue('title');
                $name = $form->getValue('name');
                $about_page = $form->getValue('about_page');
                $meta_desc = $form->getValue('meta_desc');
                $meta_keyw = $form->getValue('meta_keyw');
                $block_first = $form->getValue('block_first');
                $foreach_first = $form->getValue('foreach_first');
                $foreach_last = $form->getValue('foreach_last');
                $block_second = $form->getValue('block_second');
                $showin_about = $form->getValue('showin_about');
                $showin_services = $form->getValue('showin_services');
                $showin_news = $form->getValue('showin_news');
                $this->subPageDb->insertPage($title, $name, $about_page,
                        $meta_desc, $meta_keyw, $block_first, $foreach_first,
                        $foreach_last, $block_second, $showin_about,
                        $showin_services, $showin_news);
                $this->_helper->redirector('edit-pages');
            } else {
                $form->populate($formData);
            }
        }
    }

    public function addContentAction() {
        $pageId = $this->_getParam('pg', 0);
        $form = new Control_Model_Form_SubPageContent();
        $form->submit->setLabel('Add');
        $this->view->form = $form;
//        $form->populate($formData);
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id_page = $pageId;
                $content = $form->getValue('content');
                $this->subContentDb->insertPageContent($id_page, $content);
                //echo 'Success! <a href="/control/sub-page/edit-pages/id/'.$pageId.'">Back</a>';exit();
                $this->_helper->redirector('edit-pages','sub-page','control', array('id' => $pageId));
            } else {
                $form->populate($formData);
            }
        }
    }

}

